package utils;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;

public class SqlUtils {

    private static final String UNDERLINE = "_";

    private static final String APOSTROPHE = "'";

    private static final String COMMA = ", ";

    private static final String LEFT_BRACKET = "(";

    private static final String RIGHT_BRACKET = ")";

    private static final String SEMICOLON = ";";

    /**
     * 批量插入
     *
     * @param list      对象集合
     * @param tableName 表名
     */
    public static List<String> joinBatchInsert(List list, String tableName) {
        List<String> sqls = new ArrayList<>();

        StringBuilder br1 = new StringBuilder(joinSqlColumnName(list.get(0).getClass(), tableName)); // insert ..... values
        String sqlHalf = br1.toString();

        StringBuilder br = new StringBuilder();

        out: // values(.....)
        for (int i = 0; i < list.size(); i++) {
            br.append(LEFT_BRACKET);
            Object o = list.get(i);
            Field[] fields = o.getClass().getDeclaredFields();

            for (int j = 0; j < fields.length; j++) {
                Field field = fields[j];
                try {
                    field.setAccessible(Boolean.TRUE);
                    Object value = field.get(o);
                    String name = field.getType().getSimpleName();
                    if ("String".equals(name)) {
                        br.append(Objects.isNull(value) ? "NULL" + COMMA : APOSTROPHE + value + APOSTROPHE + COMMA);
                    } else if ("Integer".equals(name) || "double".equals(name)) {
                        br.append(value + COMMA);
                    } else if ("Date".equals(name)) {
                        br.append("now()" + COMMA);
                    } else {
                        System.out.println("===========缺少类型===========" + name);
                        break out;
                    }
                } catch (IllegalAccessException e) {
                    System.out.println("===========Exception===========" + e);
                    break out;
                }
            }
            String sql = sqlHalf +  br.replace(br.length() - 2, br.length(), RIGHT_BRACKET + ";"); // 拼接 前后半段sql
            sqls.add(sql);
            br = new StringBuilder();
        }

        return sqls;
    }

    /**
     * 拼接sql插入字段名
     *
     * @param clazz     实体类类型
     * @param tableName 表名
     * @return
     */
    private static String joinSqlColumnName(Class clazz, String tableName) {
        StringBuilder br = new StringBuilder("INSERT INTO " + tableName + LEFT_BRACKET);
        Field[] fields = clazz.getDeclaredFields();
        for (int i = 0; i < fields.length; i++) {
            br.append(fieldConvertColumn(fields[i].getName()) + COMMA);
        }
        return br.substring(0, br.length() - 2) + ") VALUES";
    }

    /**
     * java对象属性名转数据库字段名
     *
     * @param attribute 属性名
     * @return
     */
    private static String fieldConvertColumn(String attribute) {
        StringBuilder br = new StringBuilder();
        for (int i = 0; i < attribute.length(); i++) {
            char c = attribute.charAt(i);
            if (Character.isUpperCase(c)) {
                br.append(UNDERLINE);
            }
            br.append(c);
        }
        return br.toString().toLowerCase();
    }
}